import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:part_time_job_pro/common/index.dart';
import 'package:smart_alert_dialog/models/alert_dialog_style.dart';
import 'package:smart_alert_dialog/models/alert_dialog_text.dart';
import 'package:smart_alert_dialog/smart_alert_dialog.dart';

import 'logic.dart';

class PartJobPage extends StatefulWidget {
  const PartJobPage({Key? key}) : super(key: key);

  //路由地址
  static const String routeName = "/public/part";

  @override
  State<PartJobPage> createState() => PartJobPageState();
}

//路由地址
class PartJobPageState extends State<PartJobPage> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    Get.delete<PartJobLogic>();
    super.dispose();
  }

  @override
  void didChangeMetrics() {
    //处理键盘收缩
    if (MediaQuery.of(context).viewInsets.bottom == 0) {
      state.isShowNextStepButton = true;
      logic.update();
    } else {
      state.isShowNextStepButton = false;
      logic.update();
    }
  }

  final logic = Get.find<PartJobLogic>();
  final state = Get.find<PartJobLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PartJobLogic>(builder: (logic) {
      return Scaffold(
        resizeToAvoidBottomInset: true, // 添加这一行处理键盘遮挡
        appBar: AppBar(
          backgroundColor: AppColor.norWhite01Color,
          leading: InkWell(
              onTap: () {
                Get.back();
              },
              child: Container(
                padding: EdgeInsets.all(0.r),
                child: Image.asset(AssetsImages.appBarBack),
              )),
          elevation: 0,
          title: const Text(
            "填写基本信息",
            style: AppTheme.appBarStyle,
          ),
          actions: [
            Padding(
                padding: const EdgeInsets.fromLTRB(0, 15, 15, 10),
                child: Text(
                  "帮助文档",
                  style: TextStyle(fontSize: 14.r, color: AppColor.norBlue02Colors),
                ))
          ],
        ),
        body: GestureDetector(
          onTap: () {
            CommonUtil.clearFocus();
          },
          child: Stack(
            children: [
              Scrollbar(
                  thickness: state.scrollBarThickness,
                  thumbVisibility: true,
                  trackVisibility: true,
                  child: SingleChildScrollView(
                      controller: logic.scrollController,
                      child: Container(
                        width: 1.sw,
                        margin: EdgeInsets.only(bottom: 80.h, top: 4.h),
                        color: AppColor.norWhite01Color,
                        padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 0.r),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            buildJobName(),
                            buildJobTypeSelect(context),
                            buildDetailContent(context),
                            buildWorkScenes(),
                            buildWorkCitySelect(context),
                            buildDetailAddress(),
                          ],
                        ),
                      ))),
              Positioned(
                top: 0.h,
                left: 0.h,
                child: buildTopProgress(),
              ),
              Positioned(
                bottom: 0,
                left: 0,
                child: Visibility(visible: state.isShowNextStepButton, child: buildGoNextButton()),
              )
            ],
          ),
        ),
      );
    });
  }

  //处理的顶部进度条
  Widget buildTopProgress() {
    return Container(
      height: 4.h,
      width: 1.sw,
      color: AppColor.norWhite01Color,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Expanded(
              flex: 1,
              child: Container(
                  height: 3.h,
                  decoration: BoxDecoration(
                      color: AppColor.norMainThemeColors,
                      borderRadius:
                          BorderRadius.only(topRight: Radius.circular(4.r), bottomRight: Radius.circular(4.r))))),
          Expanded(
              flex: 1, child: Container(height: 3.h, decoration: const BoxDecoration(color: AppColor.norWhite01Color))),
        ],
      ),
    );
  }

  //处理职位名称
  Widget buildJobName() {
    return Container(
        padding: EdgeInsets.symmetric(vertical: 0.r, horizontal: 20.r),
        width: 1.sw,
        child: Column(
          children: [
            Container(
              color: AppColor.norWhite01Color,
              child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                Expanded(
                    flex: 1,
                    child: Container(
                      alignment: Alignment.centerLeft,
                      width: formTitleWidth.w,
                      child: const Text("职位名称", style: AppTheme.textTitleStyle),
                    )),
                Container(
                  alignment: Alignment.centerRight,
                  child: Text("${state.jobNameLength}/20",
                      textAlign: TextAlign.end, style: TextStyle(fontSize: 13.sp, color: AppColor.norGray02Color)),
                )
              ]),
            ),
            Container(
              decoration: BoxDecoration(
                color: AppColor.norWhite01Color,
                border: Border(
                  bottom: BorderSide(width: 1.r, color: AppColor.norWhite06Color),
                ),
              ),
              child: TextField(
                focusNode: logic.jobNameFocusNode,
                keyboardType: TextInputType.text,
                keyboardAppearance: Brightness.light,
                autofocus: true,
                showCursor: true,
                cursorHeight: 16.sp,
                onChanged: (text) {
                  logic.updateJobName(text);
                },
                cursorColor: AppColor.norYellow04Colors,
                controller: logic.jobNameTextEditController,
                decoration: InputDecoration(
                    hintText: "请输入职位名称",
                    border: InputBorder.none,
                    suffixIcon: state.jobName.isNotEmpty && logic.jobNameFocusNode.hasFocus
                        ? IconButton(
                            color: AppColor.norGrayColor,
                            onPressed: () {
                              logic.clearJobName();
                            },
                            icon: const Icon(Icons.cancel))
                        : SizedBox(
                            width: 0.r,
                          ),
                    hintStyle: TextStyle(
                      color: AppColor.norGrayColor,
                      fontSize: 14.sp,
                    )),
                style: TextStyle(fontSize: 13.sp, color: AppColor.norBlackColors),
              ),
            )
          ],
        ));
  }

  //职位类型选择
  Widget buildJobTypeSelect(BuildContext context) {
    return Container(
        padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 20.r),
        color: AppColor.norWhite01Color,
        width: 1.sw,
        child: Column(
          children: [
            Container(
              alignment: Alignment.centerLeft,
              width: double.infinity,
              child: const Text("职位类型", style: AppTheme.textTitleStyle),
            ),
            Container(
              decoration: BoxDecoration(
                color: AppColor.norWhite01Color,
                border: Border(
                  bottom: BorderSide(width: 1.r, color: AppColor.norWhite06Color),
                ),
              ),
              child: Row(children: [
                Expanded(
                    child: InkWell(
                        onTap: () {
                          CommonUtil.clearFocus();
                          showJobTypeSelect(context);
                        },
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 10.r),
                          margin: EdgeInsets.only(left: 0.r),
                          child: Text(
                            state.selectPartTypeText == "" ? "请选择" : state.selectPartTypeText,
                            style: TextStyle(
                                fontSize: 13.sp,
                                color:
                                    state.selectPartTypeText == "" ? AppColor.norGrayColor : AppColor.norBlackColors),
                          ),
                        ))),
                Container(
                    alignment: Alignment.centerRight,
                    width: 40.w,
                    child: Image.asset(
                      AssetsImages.rightArrow,
                      height: 14.h,
                    ))
              ]),
            ),
          ],
        ));
  }

  //处理地区选择
  Widget buildWorkCitySelect(BuildContext context) {
    return Container(
        padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 20.r),
        color: AppColor.norWhite01Color,
        width: 1.sw,
        child: Column(
          children: [
            Container(
              alignment: Alignment.centerLeft,
              child: const Text("工作地点", style: AppTheme.textTitleStyle),
            ),
            Container(
              decoration: BoxDecoration(
                color: AppColor.norWhite01Color,
                border: Border(
                  bottom: BorderSide(width: 1.r, color: AppColor.norWhite06Color),
                ),
              ),
              child: Row(children: [
                Expanded(
                    child: InkWell(
                        onTap: () {
                          CommonUtil.clearFocus();
                          logic.showCitySelector(context);
                        },
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 10.r),
                          child: Text(
                            state.countryText == "" ? "请选择" : state.countryText,
                            style: TextStyle(
                                fontSize: 13.sp,
                                color: state.countryText == "" ? AppColor.norGrayColor : AppColor.norBlackColors),
                          ),
                        ))),
                Container(
                    alignment: Alignment.centerRight,
                    width: 40.r,
                    child: Image.asset(
                      AssetsImages.rightArrow,
                      height: 14.h,
                    ))
              ]),
            ),
          ],
        ));
  }

  //处理详细地址
  Widget buildDetailAddress() {
    return Container(
        padding: EdgeInsets.symmetric(vertical: 0.r, horizontal: 20.r),
        width: 1.sw,
        child: Column(
          children: [
            Container(
              alignment: Alignment.centerLeft,
              child: const Text("详细地址", style: AppTheme.textTitleStyle),
            ),
            Container(
              decoration: BoxDecoration(
                color: AppColor.norWhite01Color,
                border: Border(
                  bottom: BorderSide(width: 1.r, color: AppColor.norWhite06Color),
                ),
              ),
              child: TextField(
                focusNode: logic.addressTextFocusNode,
                keyboardType: TextInputType.text,
                keyboardAppearance: Brightness.light,
                autofocus: true,
                showCursor: true,
                cursorHeight: 16.sp,
                onChanged: (text) {
                  logic.updateAddress(text);
                },
                onEditingComplete: () {
                  print("edit over！！！");
                },
                onSubmitted: (text) {
                  print("sdfsdfsf submit");
                },
                onTapOutside: (event) {
                  print("tab");
                },
                cursorColor: AppColor.norYellow04Colors,
                controller: logic.addressTextEditController,
                decoration: InputDecoration(
                    hintText: "请输入详细地址（精确到楼层或门牌号）",
                    border: InputBorder.none,
                    suffixIcon: state.addressText.isNotEmpty && logic.addressTextFocusNode.hasFocus
                        ? IconButton(
                            color: AppColor.norGrayColor,
                            onPressed: () {
                              logic.clearAddress();
                            },
                            icon: const Icon(Icons.cancel))
                        : SizedBox(
                            width: 0.r,
                          ),
                    hintStyle: TextStyle(
                      color: AppColor.norGrayColor,
                      fontSize: 14.sp,
                    )),
                style: TextStyle(fontSize: 13.sp, color: AppColor.norBlackColors),
              ),
            )
          ],
        ));
  }

  //处理招聘内容
  Widget buildDetailContent(BuildContext context) {
    return Container(
        padding: EdgeInsets.symmetric(vertical: 0.r, horizontal: 20.r),
        width: 1.sw,
        child: Column(children: [
          Container(
            color: AppColor.norWhite01Color,
            child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
              Expanded(
                  flex: 1,
                  child: Container(
                    alignment: Alignment.centerLeft,
                    width: formTitleWidth.w,
                    child: const Text("招聘内容", style: AppTheme.textTitleStyle),
                  )),
              Container(
                alignment: Alignment.centerRight,
                child: const Text("1/2000",
                    textAlign: TextAlign.end, style: TextStyle(fontSize: 13, color: AppColor.norGray02Color)),
              )
            ]),
          ),
          SizedBox(
            height: 10.h,
          ),
          Text(
            "1、请勿输入QQ/电话/微信/邮箱/网址等联系方式以及特俗符号，否则会被下架处理；",
            style: TextStyle(color: AppColor.norGray02Color, fontSize: 12.sp),
          ),
          Text(
            "2、请勿发布违反劳动法相关内容，否则您的账号会被封号处理。",
            style: TextStyle(color: AppColor.norGray02Color, fontSize: 12.sp),
          ),
          SizedBox(
            height: 10.h,
          ),
          Row(mainAxisAlignment: MainAxisAlignment.end, children: [
            GestureDetector(
                onTap: () {},
                child: Image.asset(
                  AssetsImages.editIcon,
                  height: 14.h,
                  width: 14.w,
                )),
            GestureDetector(
                onTap: () {
                  showJobTypeTempSelect(context);
                },
                child: Text(" 查看别人怎么写", style: TextStyle(fontSize: 12.sp, color: AppColor.norGray02Color))),
            SizedBox(
              width: 15.h,
            ),
            GestureDetector(
                onTap: () {
                  logic.clearWorkContent();
                },
                child: Image.asset(
                  AssetsImages.clearIcon,
                  height: 14.h,
                  width: 14.w,
                )),
            GestureDetector(
                onTap: () {
                  logic.clearWorkContent();
                },
                child: Text(" 清空", style: TextStyle(fontSize: 12.sp, color: AppColor.norGray02Color))),
          ]),
          buildMultiTextEdit(),
        ]));
  }

  //处理场景部分
  Widget buildWorkScenes() {
    if (state.selectedPartTypeId != null && state.selectedPartTypeId != 0) {
      return Container(
        padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 20.r),
        width: 1.sw,
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(width: 1.r, color: AppColor.norWhite06Color),
          ),
        ),
        child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
          Container(
            alignment: Alignment.centerRight,
            child: const Text("工作场景", textAlign: TextAlign.end, style: AppTheme.textTitleStyle),
          ),
          Expanded(
              flex: 1,
              child: Container(
                alignment: Alignment.centerLeft,
                width: formTitleWidth.w,
                padding: EdgeInsets.only(left: 10.r),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: buildWorkSceneList(state.selectedPartTypeId!),
                ),
              )),
        ]),
      );
    } else {
      return const SizedBox(
        height: 0,
      );
    }
  }

  //处理场景显示
  List<Widget> buildWorkSceneList(int typeId) {
    List<Widget> sceneList = [];
    PartJobType jobType = logic.findPartJobTypeById(typeId);
    if (jobType.sceneList != null) {
      for (WorkScene scene in jobType.sceneList!) {
        //处理场景默认值
        if (scene.checked == true) {
          state.selectedScene = scene.id!;
          //LogUtil.e(scene.id, tag:"场景默认值为:");
        }
        sceneList.add(buildSingleSceneButton(scene, 10.0));
      }
    }
    return sceneList;
  }

  //处理单个场景按钮
  Widget buildSingleSceneButton(WorkScene scene, leftPadding) {
    return scene.disabled == true
        ? Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(vertical: 5.r, horizontal: 15.r),
            margin: EdgeInsets.only(left: leftPadding),
            decoration: BoxDecoration(color: AppColor.diabledButtonColor, borderRadius: BorderRadius.circular(4)),
            child: Text(scene.name ?? "", style: TextStyle(color: AppColor.norGray02Color, fontSize: 13.sp)),
          )
        : GestureDetector(
            onTap: () {
              logic.updateWorkSceneChecked(scene);
            },
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(vertical: 5.r, horizontal: 15.r),
              margin: EdgeInsets.only(left: leftPadding),
              decoration: BoxDecoration(
                  color: scene.checked == true ? AppColor.norYellow04Colors : AppColor.norWhite01Color,
                  border: Border.all(
                    width: 0.5.r,
                    color: scene.checked == true ? AppColor.norWhite01Color : AppColor.norGray04Color,
                  ),
                  borderRadius: BorderRadius.circular(4)),
              child: Text(scene.name ?? "",
                  style: TextStyle(
                      color: scene.checked == true ? AppColor.norWhite01Color : AppColor.norGray04Color,
                      fontSize: 13.sp)),
            ));
  }

  //处理多行文本输入
  Widget buildMultiTextEdit() {
    return SingleChildScrollView(
      child: Container(
        color: AppColor.norGray01Color,
        height: 200.h,
        margin: EdgeInsets.symmetric(vertical: 10.h, horizontal: 0.r),
        padding: EdgeInsets.all(10.r),
        child: TextField(
          maxLines: null,
          focusNode: logic.workContentFocusNode,
          controller: logic.workContentEditController,
          onChanged: (String value) {
            logic.updateWorkContent(value);
          },
          decoration: InputDecoration(
            contentPadding: EdgeInsets.only(left: 0.r),
            hintText: '【工资薪水】\n【工作内容】\n【上班时间】\n【工作地点】\n【工作要求】\n【其他福利】',
            border: InputBorder.none,
          ),
        ),
      ),
    );
  }

  //职位类型选择
  showJobTypeSelect(BuildContext context) {
    //初始化窗口
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      isDismissible: false,
      // shape: RoundedRectangleBorder(
      //   borderRadius: BorderRadius.only(
      //       topLeft: Radius.circular(0.r), topRight: Radius.circular(0.r)),
      // ),
      builder: (context) => GetBuilder<PartJobLogic>(
        builder: (logic) => Container(
          height: 1.sh * 0.65, //这里调整高度即可，建议按照屏幕高度比例来计算
          color: Colors.white,
          child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                buildTypeSelectHeader(),
                state.operator == "select" ? buildCenterContent() : buildSearchContent(context),
                buildTypeSelectFooter()
              ]),
        ),
      ),
    );
    Future.delayed(
      const Duration(milliseconds: 300),
      () {
        logic.initPartJobTypeCheck();
      },
    );
  }

  //职位类型选择
  showJobTypeTempSelect(BuildContext context) {
    if (!state.partTypeTempList.isNotEmpty) {
      EasyLoading.showToast("请先选择职业类型!");
      return;
    }
    //初始化窗口
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      isDismissible: false,
      // shape: RoundedRectangleBorder(
      //   borderRadius: BorderRadius.only(
      //       topLeft: Radius.circular(0.r), topRight: Radius.circular(0.r)),
      // ),
      builder: (context) => GetBuilder<PartJobLogic>(
        builder: (logic) => Container(
          height: 1.sh * 0.65, //这里调整高度即可，建议按照屏幕高度比例来计算
          color: Colors.white,
          child: Stack(children: [
            Positioned(top: 0.r, left: 0.r, child: buildTypeTempSelectHeader()),
            Positioned(top: 45.r, left: 0.r, child: buildTypeTempContent()),
            Positioned(bottom: 0.r, left: 0.r, child: buildTypeTempSelectFooter(context)),
          ]),
        ),
      ),
    );
  }

  Widget buildTypeTempContent() {
    return Container(
      height: 1.sh * 0.65 - 45.r - 50.r,
      width: 1.sw,
      color: Colors.white,
      padding: EdgeInsets.symmetric(vertical: 15.h, horizontal: 15.w),
      child: SingleChildScrollView(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "${state.tempPartTypeName}",
            textAlign: TextAlign.start,
            style: TextStyle(fontSize: 16.r, fontWeight: FontWeight.bold, color: AppColor.norBlack01Colors),
          ),
          SizedBox(
            height: 5.h,
          ),
          Text(
            "以下是模板内容，点击粘贴将覆盖原有内容",
            textAlign: TextAlign.start,
            style: TextStyle(fontSize: 13.r, fontWeight: FontWeight.normal, color: AppColor.norGray02Color),
          ),
          SizedBox(
            height: 15.h,
          ),
          Text(
            state.showTempContent,
            textAlign: TextAlign.start,
            style: TextStyle(fontSize: 13.r, fontWeight: FontWeight.w500, color: AppColor.norGray05Color),
          )
        ],
      )),
    );
  }

  ///类型模板选择头部
  Widget buildTypeTempSelectHeader() {
    return Container(
      height: 45.r,
      width: 1.sw,
      color: Colors.white,
      padding: EdgeInsets.all(5.r),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          GestureDetector(
            onTap: () {
              Get.back();
            },
            child: Container(
              alignment: Alignment.centerLeft,
              width: 40.r,
              color: Colors.white,
              child: Image.asset(
                AssetsImages.closeGray,
                height: 42.r,
              ),
            ),
          ),
          Expanded(
            flex: 1,
            child: Text(
              "选择类型",
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 14.r, color: AppColor.norBlack01Colors),
            ),
          ),
        ],
      ),
    );
  }

  ///类型选择头部
  Widget buildTypeSelectHeader() {
    return Container(
      height: 45.r,
      width: 1.sw,
      color: Colors.white,
      padding: EdgeInsets.all(5.r),
      child: Stack(
        fit: StackFit.loose,
        children: [
          Positioned(
              top: 0.h,
              left: 0.w,
              child: GestureDetector(
                  onTap: () {
                    logic.clearListChecked("0");
                    Get.back();
                  },
                  child: Container(
                      alignment: Alignment.centerLeft,
                      width: 40.r,
                      color: Colors.white,
                      child: Image.asset(
                        AssetsImages.closeGray,
                        height: 42.r,
                      )))),
          Positioned(
              left: (1.sw - 160.w) / 2,
              top: 5.h,
              child: Container(
                height: 25.r,
                width: 160.r,
                decoration: BoxDecoration(
                  color: AppColor.norGray01Color,
                  borderRadius: BorderRadius.all(Radius.circular(20.r)),
                ),
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      GestureDetector(
                          onTap: () {
                            state.operator = "select";
                            logic.update();
                          },
                          child: Container(
                              height: 25.r,
                              width: 80.r,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                color:
                                    state.operator == "select" ? AppColor.norMainThemeColors : AppColor.norGray01Color,
                                borderRadius: BorderRadius.all(Radius.circular(20.r)),
                              ),
                              child: Text(
                                "选择类型",
                                style: TextStyle(
                                    fontSize: 12.r,
                                    color: state.operator == "select"
                                        ? AppColor.norWhite01Color
                                        : AppColor.norGray02Color),
                              ))),
                      GestureDetector(
                          onTap: () {
                            state.operator = "search";
                            logic.update();
                          },
                          child: Container(
                              height: 25.r,
                              width: 80.r,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                color:
                                    state.operator == "search" ? AppColor.norMainThemeColors : AppColor.norGray01Color,
                                borderRadius: BorderRadius.all(Radius.circular(20.r)),
                              ),
                              child: Text(
                                "搜索类型",
                                style: TextStyle(
                                    fontSize: 12.r,
                                    color: state.operator == "search"
                                        ? AppColor.norWhite01Color
                                        : AppColor.norGray02Color),
                              ))),
                    ]),
              )),
          Visibility(
              visible: state.isShowAddTypeBtn,
              child: Positioned(
                  top: 5.h,
                  right: 10.w,
                  child: GestureDetector(
                      onTap: () {},
                      child: Text(
                        "完成",
                        style: TextStyle(fontSize: 14.r, color: AppColor.norMainThemeColors),
                      )))),
        ],
      ),
    );
  }

  //处理搜索类型
  Widget buildSearchContent(BuildContext context) {
    if (state.isShowAddTypeBtn) {
      return buildAddJobName();
    } else {
      return Container(
        height: 1.sh * 0.65 - 45.r,
        width: 1.sw,
        color: Colors.white,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [buildSearchTextInput(context), buildSearchTypeList()],
        ),
      );
    }
  }

  //处理新增职位名称
  Widget buildAddJobName() {
    return Container(
        padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 20.r),
        width: 1.sw,
        child: Column(
          children: [
            Container(
              color: AppColor.norWhiteColor,
              child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                Expanded(
                    flex: 1,
                    child: Container(
                      alignment: Alignment.centerLeft,
                      width: formTitleWidth.w,
                      child: const Text("申请新的职位类型", style: AppTheme.textTitleStyle),
                    )),
                Container(
                  alignment: Alignment.centerRight,
                  child: const Text("1/20",
                      textAlign: TextAlign.end, style: TextStyle(fontSize: 13, color: AppColor.norGray02Color)),
                )
              ]),
            ),
            Container(
              decoration: BoxDecoration(
                color: AppColor.norWhiteColor,
                border: Border(
                  bottom: BorderSide(width: 0.2.r, color: AppColor.norGray05Color),
                ),
              ),
              child: TextField(
                focusNode: logic.addJobNameFocusNode,
                keyboardType: TextInputType.text,
                keyboardAppearance: Brightness.light,
                autofocus: true,
                showCursor: true,
                cursorHeight: 16.sp,
                onChanged: (text) {
                  logic.updateJobName(text);
                },
                // inputFormatters: <TextInputFormatter>[
                //   //LengthLimitingTextInputFormatter(20)],
                cursorColor: AppColor.norYellow04Colors,
                controller: logic.addJobNameTextEditController,
                decoration: InputDecoration(
                    hintText: "请填写您期待的岗位类型",
                    border: InputBorder.none,
                    suffixIcon: state.addJobName.isNotEmpty && logic.addJobNameFocusNode.hasFocus
                        ? IconButton(
                            color: AppColor.norGrayColor,
                            onPressed: () {
                              logic.clearAddJobName();
                            },
                            icon: const Icon(Icons.cancel))
                        : SizedBox(
                            width: 0.r,
                          ),
                    hintStyle: TextStyle(
                      color: AppColor.norGrayColor,
                      fontSize: 14.sp,
                    )),
                style: TextStyle(fontSize: 13.sp, color: AppColor.norBlackColors),
              ),
            )
          ],
        ));
  }

  //处理搜索输入框
  Widget buildSearchTextInput(BuildContext context) {
    return Container(
      height: 40.h,
      margin: EdgeInsets.symmetric(horizontal: 10.r),
      decoration: BoxDecoration(
        color: AppColor.norWhiteColor,
        border: Border(
          bottom: BorderSide(
              width: 0.5.r,
              color: logic.jobSearchFocusNode.hasFocus ? AppColor.norMainThemeColors : AppColor.norWhite06Color),
        ),
      ),
      child: TextField(
        focusNode: logic.jobSearchFocusNode,
        keyboardType: TextInputType.text,
        keyboardAppearance: Brightness.light,
        autofocus: false,
        showCursor: true,
        cursorHeight: 16.sp,
        onChanged: (text) {
          logic.updateJobSearchName(text);
        },
        onSubmitted: (val) {
          CommonUtil.clearFocus();
          logic.updateJobSearchName(val);
        },
        cursorColor: AppColor.norYellow04Colors,
        controller: logic.jobSearchEditController,
        decoration: InputDecoration(
            hintText: "请输入职位类型名称",
            border: InputBorder.none,
            prefixIcon: Icon(
              Icons.search,
              color: logic.jobSearchFocusNode.hasFocus ? AppColor.norMainThemeColors : AppColor.norGray02Color,
            ),
            suffixIcon: state.jobSearchName.isNotEmpty && logic.jobSearchFocusNode.hasFocus
                ? IconButton(
                    color: AppColor.norGrayColor,
                    onPressed: () {
                      CommonUtil.clearFocus();
                      logic.clearJobSearchName();
                    },
                    icon: const Icon(Icons.cancel))
                : SizedBox(
                    width: 0.r,
                  ),
            hintStyle: TextStyle(
              color: AppColor.norGrayColor,
              fontSize: 14.sp,
            )),
        style: TextStyle(fontSize: 13.sp, color: AppColor.norBlackColors),
      ),
    );
  }

  //处理搜索列表
  Widget buildSearchTypeList() {
    if (state.searchDataList.isEmpty && state.jobSearchName.isNotEmpty) {
      return Container(
          margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 20.h),
          color: AppColor.norWhiteColor,
          alignment: Alignment.center,
          child: Text.rich(TextSpan(children: [
            TextSpan(
              text: "没有找到符合的职位类型，你可以",
              style: TextStyle(color: AppColor.norGray02Color, fontSize: 12.sp),
            ),
            TextSpan(
              text: "点此申请新的类型",
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  state.isShowAddTypeBtn = !state.isShowAddTypeBtn;
                  logic.update();
                },
              style: TextStyle(color: AppColor.norBlue04Colors, fontSize: 12.sp),
            ),
          ])));
    }
    return Container(
      height: 1.sh * 0.55 - 45.h,
      margin: EdgeInsets.symmetric(horizontal: 10.r),
      child: ListView.builder(
          itemCount: state.searchDataList.length,
          itemBuilder: (context, index) {
            return GestureDetector(
                onTap: () {
                  logic.clickSearchList(state.searchDataList[index]);
                },
                child: Column(
                  children: [
                    Container(
                      width: double.infinity,
                      padding: EdgeInsets.only(top: 10.r),
                      child: Text(
                        "${state.searchDataList[index].typeName}",
                        style: TextStyle(fontSize: 14.r, fontWeight: FontWeight.w500, color: AppColor.norBlack01Colors),
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            width: 0.5.r,
                            color: AppColor.norWhite06Color,
                          ),
                        ),
                      ),
                      padding: EdgeInsets.symmetric(horizontal: 0, vertical: 5.r),
                      child: Text(
                        "${state.searchDataList[index].parentName} -> ${state.searchDataList[index].typeName}",
                        style: TextStyle(fontSize: 12.r, fontWeight: FontWeight.normal, color: AppColor.norGray02Color),
                      ),
                    ),
                  ],
                ));
          }),
    );
  }

  //中间部分
  Widget buildCenterContent() {
    return Container(
      color: Colors.white,
      height: 1.sh * 0.55 - 50.h,
      width: 1.sw,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [buildTypeSelectLeft(), buildTypeSelectRight()],
      ),
    );
  }

  ///类型选择左边
  Widget buildTypeSelectLeft() {
    if (state.firstLevelList.isNotEmpty) {
      return Container(
        width: 80.w,
        padding: EdgeInsets.only(top: 10.h, bottom: 10.h),
        decoration: BoxDecoration(
            color: AppColor.norGray01Color,
            borderRadius: BorderRadius.only(topLeft: Radius.circular(10.w), bottomLeft: Radius.circular(10.w))),
        child: ListView.builder(
          itemCount: state.firstLevelList.length,
          itemBuilder: (context, index) {
            return GestureDetector(
              onTap: () {
                logic.changFisrtChecked(state.firstLevelList[index].typeId);
              },
              child: Stack(
                children: [
                  Container(
                    height: 30.h,
                    width: double.infinity,
                    alignment: Alignment.center,
                    color: state.firstLevelList[index].checked == true ? AppColor.norWhiteColor : Colors.transparent,
                    padding: EdgeInsets.only(left: 10.w),
                    child: Text(
                      "${state.firstLevelList[index].typeName}",
                      style: state.firstLevelList[index].checked == true
                          ? TextStyle(fontSize: 14.r, fontWeight: FontWeight.bold, color: AppColor.norBlack01Colors)
                          : TextStyle(fontSize: 12.r, fontWeight: FontWeight.w500, color: AppColor.norBlackColors),
                    ),
                  ),
                  Visibility(
                    visible: state.firstLevelList[index].checked == true,
                    child: Positioned(
                      left: 0,
                      top: 5.h,
                      child: Container(
                        height: 20.r,
                        width: 2.r,
                        color: AppColor.norMainThemeColors,
                        child: const SizedBox(
                          height: 10,
                          width: 4,
                        ),
                      ),
                    ),
                  )
                ],
              ),
            );
          },
        ),
      );
    } else {
      return Container(
        color: Colors.white,
        width: 80.w,
        height: double.infinity,
      );
    }
  }

  ///类型选择右边
  Widget buildTypeSelectRight() {
    if (state.secondLevelList.isNotEmpty) {
      return Container(
          width: 1.sw - 80.w,
          padding: EdgeInsets.all(0.h),
          decoration: BoxDecoration(
              color: AppColor.norWhiteColor,
              borderRadius: BorderRadius.only(topLeft: Radius.circular(10.w), bottomLeft: Radius.circular(10.w))),
          child: ListView(
            children: buildRigtRowWidget(),
          ));
    } else {
      return Container(
        color: Colors.white,
        width: 80.w,
        height: double.infinity,
      );
    }
  }

  //处理右侧每行显示
  List<Widget> buildRigtRowWidget() {
    List<Widget> rowList = [];
    //二级分组数据
    List rowData = logic.dealListData(state.secondLevelList);
    if (rowData.isNotEmpty) {
      for (int i = 0; i < rowData.length; i++) {
        List<Widget> subList = getRowChildItem(rowData[i], i);
        rowList.add(
          Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: subList),
              //buildThirdArrow(i),
              buildGridView(i) //传递行号
            ],
          ),
        );
      }
    }
    return rowList;
  }

  //处理gridView部分
  Widget buildGridView(rowIndex) {
    //判断选中上级
    bool flag = state.secondCheckedType?.parentId == state.firstCheckedType?.typeId;
    //LogUtil.e(flag, tag: "flag");
    if (state.checkedRowIndex == rowIndex && state.thirdLevelList.isNotEmpty && flag) {
      //计算gridView元素宽高比例
      double leftWidth = 80.w;
      //右侧宽高=总宽度-左侧宽度-GridView中间的间距
      var rightItemWidth = (1.sw - leftWidth - 10.w - 10.w) / 3;
      var rightItemHeight = 35.h;

      //计算背景高度
      double viewHeight = 0.0;
      List subList = logic.dealListData(state.thirdLevelList, count: 3);
      //LogUtil.e(subList.length, tag: "gridview数据行数>>>");
      if (subList.isNotEmpty) {
        int len = subList.length;
        switch (len) {
          case 1:
            viewHeight = len * 40.h + 25.h;
            break;
          case 2:
            viewHeight = len * 40.h + 20.h;
            break;
          case 3:
            viewHeight = len * 40.h + 15.h;
            break;
          case 4:
            viewHeight = len * 40.h + 10.h;
            break;
          default:
            viewHeight = 180.h;
            break;
        }
      }
      //LogUtil.e(viewHeight, tag: "三级组件高度");

      return Container(
        //width: 1.sw - 80.w,
        height: viewHeight,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4.r),
          color: AppColor.norWhite02Color,
        ),
        margin: EdgeInsets.only(left: 10.r, right: 10.r),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              width: 1.sw - 80.w,
              padding: EdgeInsets.only(left: 10.w, top: 10.w, bottom: 2.w),
              child: Text.rich(TextSpan(children: [
                TextSpan(
                  text: "* ",
                  style: TextStyle(fontSize: 12.sp, color: AppColor.norYellow04Colors),
                ),
                TextSpan(
                  text: "兼职子类型",
                  style: TextStyle(color: AppColor.norBlack01Colors, fontSize: 12.sp),
                )
              ])),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 5.w),
              child: GridView.builder(
                physics: const ScrollPhysics(),
                primary: false,
                shrinkWrap: true,
                scrollDirection: Axis.vertical,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3,
                    mainAxisSpacing: 0.r,
                    crossAxisSpacing: 0.r,
                    childAspectRatio: rightItemWidth / rightItemHeight),
                itemCount: state.thirdLevelList.length,
                itemBuilder: (context, index) {
                  return buildThirdRightItem(state.thirdLevelList[index], index);
                },
              ),
            )
          ],
        ),
      );
    } else {
      return const SizedBox(
        height: 0,
        width: 0,
      );
    }
  }

  //二级组件
  List<Widget> getRowChildItem(List<PartJobType> childList, int rowIndex) {
    List<Widget> childWidgetList = [];
    if (childList.isNotEmpty) {
      for (int i = 0; i < childList.length; i++) {
        childWidgetList.add(buildRightItem(childList[i], rowIndex));
      }
    }
    //处理占位
    if (childWidgetList.length < 2) {
      childWidgetList.add(buildBlanKRightItem());
    }
    return childWidgetList;
  }

  //处理占位组件
  Widget buildBlanKRightItem() {
    return Container(
      width: (1.sw - 80.w - 30.w) / 2,
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            height: 30.h,
            width: (1.sw - 80.w - 30.w) / 2,
            alignment: Alignment.center,
            margin: EdgeInsets.symmetric(vertical: 5.r, horizontal: 5.r),
            child: Text(
              "占位组件",
              style: TextStyle(fontSize: 11.r, fontWeight: FontWeight.w500, color: AppColor.norWhiteColor),
            ),
          ),
          // Image.asset(
          //   AssetsImages.subArrowIcon,
          //   color: Colors.white,
          //   height: 16.r,
          //   width: 16.r,
          // )
        ],
      ),
    );
  }

  //处理单个组件
  Widget buildRightItem(PartJobType jobType, int rowIndex) {
    return GestureDetector(
      onTap: () {
        logic.changSecondChecked(jobType.typeId, rowIndex);
      },
      child: Container(
        width: (1.sw - 80.w - 30.w) / 2,
        color: Colors.white,
        child: Column(
          children: [
            Container(
              height: 30.h,
              width: (1.sw - 80.w - 30.w) / 2,
              alignment: Alignment.center,
              margin: EdgeInsets.symmetric(vertical: 5.r, horizontal: 0.r),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(4.r),
                border: Border.all(
                    width: jobType.checked == true ? 0 : 0.2,
                    color: jobType.checked == true ? Colors.transparent : AppColor.norGray03Color),
                color: jobType.checked == true ? AppColor.norMainThemeColors : AppColor.norWhiteColor,
              ),
              child: Text(
                "${jobType.typeName}",
                style: TextStyle(
                    fontSize: 12.r,
                    fontWeight: FontWeight.w500,
                    color: jobType.checked == true ? AppColor.norWhiteColor : AppColor.norBlackColors),
              ),
            ),
            Align(
                alignment: Alignment.bottomCenter,
                child: Image.asset(
                  AssetsImages.subArrowIcon,
                  color: jobType.checked == true && state.thirdLevelList.isNotEmpty
                      ? AppColor.norGray01Color
                      : Colors.transparent,
                  width: 12.r,
                ))
          ],
        ),
      ),
    );
  }

  //处理三级单个组件
  Widget buildThirdRightItem(PartJobType jobType, index) {
    return GestureDetector(
      onTap: () {
        logic.changThirdChecked(jobType.typeId!);
      },
      child: Container(
        width: (1.sw - 80.w - 30.w) / 2,
        alignment: Alignment.center,
        margin: EdgeInsets.symmetric(vertical: 5.r, horizontal: 5.r),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4.r),
          border: Border.all(
              width: jobType.checked == true ? 0.5 : 0.3,
              color: jobType.checked == true ? AppColor.norYellow04Colors : AppColor.norGray02Color),
          color: jobType.checked == true ? AppColor.norWhiteColor : AppColor.norWhiteColor,
        ),
        child: Text(
          "${jobType.typeName}",
          style: TextStyle(
              fontSize: 11.r,
              fontWeight: FontWeight.w500,
              color: jobType.checked == true ? AppColor.norYellow04Colors : AppColor.norBlackColors),
        ),
      ),
    );
  }

  ///类型选择底部
  Widget buildTypeTempSelectFooter(BuildContext context) {
    return Container(
      width: 1.sw,
      height: 80.h,
      decoration: BoxDecoration(boxShadow: [
        BoxShadow(blurRadius: 1.w, spreadRadius: 1.w, color: AppColor.norWhite01Color),
        BoxShadow(blurRadius: 3.w, spreadRadius: 3.w, color: AppColor.norGray01Color)
      ], color: Colors.white, borderRadius: BorderRadius.circular(0)),
      child: Row(
        children: [
          Visibility(
              visible: state.isShowNextButton,
              child: GestureDetector(
                onTap: () {
                  logic.getNextTempContent();
                },
                child: Container(
                  height: 35.h,
                  width: 90.w,
                  margin: EdgeInsets.only(left: 10.w, right: 5.w),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      border: Border.all(width: 1.r, color: AppColor.norMainThemeColors),
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(6)),
                  child: Text("下一个", style: TextStyle(color: AppColor.norMainThemeColors, fontSize: 14.sp)),
                ),
              )),
          Expanded(
            flex: 1,
            child: GestureDetector(
              onTap: () async {
                if (state.showTempContent != state.workContent && state.workContent != state.hinTextValue) {
                  // AwesomeDialog(
                  //   context: context,
                  //   headerAnimationLoop: false,
                  //   dialogType: DialogType.info,
                  //   title: '温馨提示',
                  //   desc: '您似乎已经编辑了一部分内容,确定要覆盖模板内容吗',
                  //   btnOkOnPress: () {
                  //     logic.submitTempContent();
                  //   },
                  //   btnCancelOnPress: () {},
                  //   btnOkIcon: Icons.check_circle,
                  //   btnCancelIcon: Icons.cancel,
                  //   btnOkText: "确定",
                  //   btnCancelText: "取消",
                  // ).show();
                  showDialog(
                    context: context,
                    builder: (_) => SmartAlertDialog(
                      title: "提示信息",
                      style: AlertDialogStyle(
                          title: TextStyle(
                            fontSize: 0.sp,
                            color: Colors.transparent,
                          ),
                          cancel: TextStyle(
                            fontSize: 14.sp,
                            color: AppColor.norBlack01Colors,
                            fontWeight: FontWeight.w500,
                          ),
                          confirm: TextStyle(
                            fontSize: 14.sp,
                            color: AppColor.norMainThemeColors,
                            fontWeight: FontWeight.w500,
                          ),
                          message: TextStyle(
                            fontSize: 13.sp,
                            fontWeight: FontWeight.w500,
                          )),
                      text: AlertDialogText(cancel: "取消", confirm: "确定"),
                      message: '您似乎已经编辑了一部分内容,确定要覆盖模板内容吗?',
                      onConfirmPressed: () => logic.submitTempContent(),
                    ),
                  );
                } else {
                  logic.submitTempContent();
                }
              },
              child: Container(
                height: 35.h,
                alignment: Alignment.center,
                margin: EdgeInsets.only(left: 5.r, right: 10.r),
                decoration: BoxDecoration(
                    color: state.secondCheckedType != null ? AppColor.norYellow04Colors : AppColor.norYellow06Colors,
                    borderRadius: BorderRadius.circular(6)),
                child: Text("粘贴到职位描述", style: TextStyle(color: AppColor.norWhite01Color, fontSize: 14.sp)),
              ),
            ),
          )
        ],
      ),
    );
  }

  ///类型选择底部
  Widget buildTypeSelectFooter() {
    return Visibility(
        visible: state.operator == "select",
        child: GestureDetector(
            onTap: () {
              logic.submitSelectType();
            },
            child: Container(
              width: 1.sw,
              height: 35.h,
              alignment: Alignment.center,
              margin: EdgeInsets.symmetric(vertical: 15.r, horizontal: 15.r),
              decoration: BoxDecoration(
                  color: state.secondCheckedType != null ? AppColor.norYellow04Colors : AppColor.norYellow06Colors,
                  borderRadius: BorderRadius.circular(6)),
              child: Text("确定", style: TextStyle(color: AppColor.norWhite01Color, fontSize: 16.sp)),
            )));
  }

  ///下一步提交按钮
  Widget buildGoNextButton() {
    return GestureDetector(
        onTap: () {
          logic.submitBasicPartTimeJob();
        },
        child: Container(
            decoration: BoxDecoration(
              color: AppColor.norWhite01Color,
              boxShadow: [
                BoxShadow(blurRadius: 1.w, spreadRadius: 1.w, color: AppColor.norWhite01Color),
                BoxShadow(blurRadius: 3.w, spreadRadius: 3.w, color: AppColor.norGray01Color)
              ],
            ),
            child: Container(
              width: 1.sw - 30.w,
              height: 35.h,
              alignment: Alignment.center,
              margin: EdgeInsets.symmetric(vertical: 20.r, horizontal: 15.r),
              decoration: BoxDecoration(color: AppColor.norYellow04Colors, borderRadius: BorderRadius.circular(6)),
              child: Text("下一步", style: TextStyle(color: AppColor.norWhite01Color, fontSize: 16.sp)),
            )));
  }
}
